COSC 220 Computer Science II
Debugging Strategies and Debugging
with gdb and valgrind
This demo will give you some practical experience with debugging using gdb.
Before you start the following tasks, go to the following link and read over the
following tutorial material:
Tasks
- Do the DebugMe (debugme.c) Code
- Compile using
'gcc -g -o debugme debugme.c'
- Run the executable by typing 'debugme "hi there" "bye bye"'
- Debug the executable
- Type 'gdb debugme'
- Read the source code by typing 'list 1' (list 10 lines starting
at line 1). To read 10 more lines, just type 'list'
- Run the program by typing 'run "hi there" "bye bye"' It
should run successfully.
- Read the source code and find the line that calls the function
print_string (around line 33). Determine the line number and set a
breakpoint there by typing 'break 33' (or whatever the line number
was).
- Step through the loop using 'next'. Each time through, examine the
values of i and argv[i] by typing 'print i' and 'print argv[i]'
- Get information about the breakpoints by typing 'info break'.
Remember the breakpoint's number.
- Delete the breakpoint by typing 'delete n' where n is the
breakpoint's number, then finish off the program by typing 'continue'
- Remake the breakpoint, then run the program as before. It will stop at
the call to print_string. This time, we'll step into the function. Type
'step' and observe that you are inside the function.
- Print the values of num and string; you should see their current
values.
- Try to print the values of i and argv. They should not be defined in
the current context since they are local to main.
- Do the CrashMe (arrayTest.cpp) Code. This program has a serious run-time bug; the array
bounds are easy to violate.
- Compile arrayTest.cpp using g++. Make sure the debug flag is set.
- Run the executable, entering various values for the index. Keep
increasing the value until a run-time fault occurs.
- Now run the executable in the debugger. You should get the run-time
error along with an indication of where it occurred.
- Do the Factorial (factorial.h,
factorial.cpp,
facDemo.cpp) Code. This will allow you to trace the call stack during
debugging.
- Compile factDemo.cpp and factorial.cpp into an executable named fact.
- Start gdb on the executable.
- Put a breakpoint in main on the call to factorial (around line 23).
- Run the program. When it stops at the breakpoint, step into the
factorial function.
- 'next' through the base cases, then 'step' into the recursive call. Do
this a few times.
- Now, take a look at the function call stack by typing 'backtrace' Note
the stack of recursive calls, each with a different value of the parameter
n.
- Now, we'll mess with the program by changing the value of a variable.
Run the program again by typing 'run'. Step into main's call to factorial.
- Inside the factorial function, print the value of n. Now change the
value of n by typing 'set variable n=5' (any value as long as it's different
from the current value of n).
- Delete the breakpoint and allow the program to complete by typing
'continue' .
- The value printed for n! should be for the changed value of n.